package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.CjlkljpsyBaDetail;
import com.ruoyi.report_wjjhl.domain.WdclphbsjBaDetailB;
import com.ruoyi.report_wjjhl.domain.WdclphbsjBaDetailC;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpsyBaDetailVo;
import com.ruoyi.report_wjjhl.domain.vo.WdclphbsjBaDetailBVo;
import com.ruoyi.report_wjjhl.domain.vo.WdclphbsjBaDetailCVo;
import com.ruoyi.report_wjjhl.mapper.CjlkljpsyBaDetailMapper;
import com.ruoyi.report_wjjhl.mapper.WdclphbsjBaDetailBMapper;
import com.ruoyi.report_wjjhl.mapper.WdclphbsjBaDetailCMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclphbsjBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclphbsjBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclphbsjBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclphbsjBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclphbsjBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料配合比设计试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-08-21
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclphbsjBsInfoServiceImpl implements IWdclphbsjBsInfoService {

    private final WdclphbsjBsInfoMapper baseMapper;
    private final CjlkljpsyBaDetailMapper detailAMapper;
    private final WdclphbsjBaDetailBMapper detailBMapper;
    private final WdclphbsjBaDetailCMapper detailCMapper;

    /**
     * 查询无机结合料稳定材料配合比设计试验检测报告
     */
    @Override
    @FIllBasicInfo
    public WdclphbsjBsInfoVo queryById(Long excelId){
        WdclphbsjBsInfoVo wdclphbsjBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询 detailA
        List<CjlkljpsyBaDetailVo> baDetailVos = detailAMapper.selectVoList(new LambdaQueryWrapper<CjlkljpsyBaDetail>()
            .eq(CjlkljpsyBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclphbsjBsInfoVo.setBaDetailVosA(baDetailVos);
        }

        // 查询 detailB
        List<WdclphbsjBaDetailBVo> baDetailVosB = detailBMapper.selectVoList(new LambdaQueryWrapper<WdclphbsjBaDetailB>()
            .eq(WdclphbsjBaDetailB::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclphbsjBsInfoVo.setBaDetailVosB(baDetailVosB);
        }

        // 查询 detailC
        List<WdclphbsjBaDetailCVo> baDetailVosC = detailCMapper.selectVoList(new LambdaQueryWrapper<WdclphbsjBaDetailC>()
            .eq(WdclphbsjBaDetailC::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclphbsjBsInfoVo.setBaDetailVosC(baDetailVosC);
        }

        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(wdclphbsjBsInfoVo.getJiancjl()) && !"检测结论：".equals(wdclphbsjBsInfoVo.getJiancjl())) {
            return wdclphbsjBsInfoVo;
        }
        return jcjl(wdclphbsjBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验检测报告列表
     */
    @Override
    public TableDataInfo<WdclphbsjBsInfoVo> queryPageList(WdclphbsjBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclphbsjBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclphbsjBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验检测报告列表
     */
    @Override
    public List<WdclphbsjBsInfoVo> queryList(WdclphbsjBsInfoBo bo) {
        LambdaQueryWrapper<WdclphbsjBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclphbsjBsInfo> buildQueryWrapper(WdclphbsjBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclphbsjBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclphbsjBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclphbsjBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), WdclphbsjBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclphbsjBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclphbsjBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclphbsjBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclphbsjBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclphbsjBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclphbsjBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclphbsjBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), WdclphbsjBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclphbsjBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclphbsjBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclphbsjBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclphbsjBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclphbsjBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), WdclphbsjBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), WdclphbsjBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), WdclphbsjBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), WdclphbsjBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclphbsjBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), WdclphbsjBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), WdclphbsjBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclphbsjBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclphbsjBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), WdclphbsjBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclphbsjBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclphbsjBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc1()), WdclphbsjBsInfo::getKuanglmc1, bo.getKuanglmc1());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc2()), WdclphbsjBsInfo::getKuanglmc2, bo.getKuanglmc2());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc3()), WdclphbsjBsInfo::getKuanglmc3, bo.getKuanglmc3());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc4()), WdclphbsjBsInfo::getKuanglmc4, bo.getKuanglmc4());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc5()), WdclphbsjBsInfo::getKuanglmc5, bo.getKuanglmc5());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc6()), WdclphbsjBsInfo::getKuanglmc6, bo.getKuanglmc6());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc7()), WdclphbsjBsInfo::getKuanglmc7, bo.getKuanglmc7());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglmc8()), WdclphbsjBsInfo::getKuanglmc8, bo.getKuanglmc8());
        lqw.eq(bo.getKuanglbl1() != null, WdclphbsjBsInfo::getKuanglbl1, bo.getKuanglbl1());
        lqw.eq(bo.getKuanglbl2() != null, WdclphbsjBsInfo::getKuanglbl2, bo.getKuanglbl2());
        lqw.eq(bo.getKuanglbl3() != null, WdclphbsjBsInfo::getKuanglbl3, bo.getKuanglbl3());
        lqw.eq(bo.getKuanglbl4() != null, WdclphbsjBsInfo::getKuanglbl4, bo.getKuanglbl4());
        lqw.eq(bo.getKuanglbl5() != null, WdclphbsjBsInfo::getKuanglbl5, bo.getKuanglbl5());
        lqw.eq(bo.getKuanglbl6() != null, WdclphbsjBsInfo::getKuanglbl6, bo.getKuanglbl6());
        lqw.eq(bo.getKuanglbl7() != null, WdclphbsjBsInfo::getKuanglbl7, bo.getKuanglbl7());
        lqw.eq(bo.getKuanglbl8() != null, WdclphbsjBsInfo::getKuanglbl8, bo.getKuanglbl8());
        lqw.eq(bo.getShuinjl() != null, WdclphbsjBsInfo::getShuinjl, bo.getShuinjl());
        lqw.eq(bo.getZuijhsl() != null, WdclphbsjBsInfo::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getZuidgmd() != null, WdclphbsjBsInfo::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getShigkzysd() != null, WdclphbsjBsInfo::getShigkzysd, bo.getShigkzysd());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料配合比设计试验检测报告
     */
    @Override
    public Boolean insertByBo(WdclphbsjBsInfoBo bo) {
        WdclphbsjBsInfo add = BeanUtil.toBean(bo, WdclphbsjBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 16次
            long size = 16;
            ArrayList<CjlkljpsyBaDetail> detailsA = new ArrayList<>((int) size);
            for (long i = 1; i <= size; i++) {
                detailsA.add(new CjlkljpsyBaDetail(add.getExcelId()));
            }
            detailAMapper.insertBatch(detailsA);

            // 新增详细 3次
            long sizeB = 3;
            ArrayList<WdclphbsjBaDetailB> detailsB = new ArrayList<>((int) sizeB);
            for (long i = 1; i <= sizeB; i++) {
                detailsB.add(new WdclphbsjBaDetailB(add.getExcelId()));
            }
            detailBMapper.insertBatch(detailsB);

            // 新增详细 6次
            long sizeC = 6;
            ArrayList<WdclphbsjBaDetailC> detailsC = new ArrayList<>((int) sizeC);
            for (long i = 1; i <= sizeC; i++) {
                detailsC.add(new WdclphbsjBaDetailC(add.getExcelId()));
            }
            detailCMapper.insertBatch(detailsC);

        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料配合比设计试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(WdclphbsjBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        WdclphbsjBsInfo update = BeanUtil.toBean(bo, WdclphbsjBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailAMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVosA(), CjlkljpsyBaDetail.class));
            detailBMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVosB(), WdclphbsjBaDetailB.class));
            detailCMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVosC(), WdclphbsjBaDetailC.class));
        }
        return flag;
    }

    private void fillExcelResult(WdclphbsjBsInfoBo bo) {
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclphbsjBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料配合比设计试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 无机结合料稳定材料配合比设计试验检测报告 检测结论
     */
    @Override
    public WdclphbsjBsInfoVo jcjl(WdclphbsjBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，4.5%水泥稳定碎石矿料比例：20-30mm：10-20mm：5-10mm：0-5mm = 20%：37%：15%：28%,"
            + " 7天无侧限抗压强度平均值5.0Mpa，代表值4.8Map，最大干密度2.364g/cm3，最佳含水率4.6%，符合《水泥稳定碎石基层技术规范》DB37/T 3577-2019"
            + "及设计要求。");
        return vo;
    }
}
